package cn.message.web;

import cn.itcast.mapper.districtMapper;
import cn.itcast.util.OutputStringController;
import cn.message.service.IDistrictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-05-11 10:00
 */
@Slf4j
@RestController
@RequestMapping(value="/dhn/message",produces="text/html;charset=UTF-8")
public class DistrictController {

    @Resource
    private IDistrictService districtService;

    @Resource
    OutputStringController osc;

    @RequestMapping("/getAllDistrict")
    @ResponseBody
    public String getAllDistrict(@RequestBody Map<String,String> map){
        String results = "";

        Map<String, Object> resultMap = districtService.getAllDistrict(map);

        //获取所有符合条件的区域列表信息
        Object districtList = resultMap.get("districtList");
        //获取符合查询条件的总记录数
        int count = (int)resultMap.get("count");

        if(districtList == null){
            results = osc.resultFailure("查询所有区域信息失败", "");
        }
        else {
            results = osc.resultSuccess("查询所有去区域信息成功",districtList, count);
        }
        return results;
    }

    @RequestMapping("/findDistrict")
    @ResponseBody
    public String findDistrict(@RequestBody Map<String,String> map){
        String results = "";

        Map<String, Object> resultMap = districtService.findDistrict(map);

        //获取所有符合条件的区域列表信息
        Object districtList = resultMap.get("districtList");
        //获取符合查询条件的总记录数
        int count = (int)resultMap.get("count");

        if(districtList == null){
            results = osc.resultFailure("查询区域信息失败", "");
        }
        else {
            results = osc.resultSuccess("查询区域信息成功",districtList, count);
        }
        return results;

    }

    /**
     * 获取所有省份信息
     * @param map
     * @return
     */
    @RequestMapping("/getAllProvince")
    @ResponseBody
    public String getAllProvince(@RequestBody Map<String,String> map){
        String results = "";

        Map<String, Object> resultMap = districtService.getAllProvince(map);

        //获取所有符合条件的区域列表信息
        Object provinceList = resultMap.get("provinceList");

        if(provinceList == null){
            results = osc.resultFailure("查询所有省份信息失败", "");
        }
        else {
            results = osc.resultSuccess("查询所有省份信息成功",provinceList);
        }
        return results;

    }

    @RequestMapping("/getChildrenDistrict")
    @ResponseBody
    public String getChildrenDistrict(@RequestBody Map<String,String> map){
        String results = "";

        Map<String, Object> resultMap = districtService.getChildrenDistrict(map);

        //获取所有符合条件的区域列表信息
        Object districtList = resultMap.get("districtList");

        if(districtList == null){
            results = osc.resultFailure("查询所有区域信息失败", "");
        }
        else {
            results = osc.resultSuccess("查询所有去区域信息成功",districtList);
        }
        return results;

    }

    @RequestMapping("/openDistrict")
    @ResponseBody
    public String openDistrict(@RequestBody Map<String,String> map){
        String results = "";
        String districtSqe = map.get("districtSqe");
        Boolean  aBoolean= districtService.openDistrict(districtSqe);
        if (!aBoolean) {
            results = osc.failure("开通失败");
        } else {
            results = osc.success("开通成功");
        }
        return results;
    }

    @RequestMapping("/closeDistrict")
    @ResponseBody
    public String closeDistrict(@RequestBody Map<String,String> map){
        String results = "";
        String districtSqe = map.get("districtSqe");
        Boolean aBoolean = districtService.closeDistrict(districtSqe);
        if (!aBoolean) {
            results = osc.failure("关闭失败");
        } else {
            results = osc.success("关闭成功");
        }
        return results;
    }
}
